import 'package:get/get.dart';
import 'package:pornhub_app/views/ai/common/ai_stepper.dart';
import 'package:pornhub_app/views/ai/common/popup/ai_wxts_image_dialog.dart';

import '../../../../components/image_picker/easy_image_picker.dart';
import '../../../../components/image_picker/easy_image_picker_file.dart';
import '../../../../components/popup/dialog/future_loading_dialog.dart';
import '../../../../http/api/api.dart';
import '../../../../http/api_code.dart';
import '../../../../services/user_service.dart';
import '../../../../utils/consts.dart';
import '../../../../utils/enum.dart';
import '../../common/ai_bytes_image.dart';
import '../../common/popup/ai_cost_gold_confirm_bottom_sheet.dart';
import '../../common/popup/ai_no_image_tips_dialog.dart';

class AiTabClothPageController extends GetxController {
  static const int stepWaitSelectImage = 0;
  static const int stepWaitSubmit = 1;
  static const int stepSubmitted = 2;
  static final steps = <AiStep>[
    AiStep('选择照片'),
    AiStep('选择并上传'),
    AiStep('一键去衣'),
  ];

  //
  final pickedImage = AiBytesImage.empty().obs;
  final step = stepWaitSelectImage.obs;
  final stepController = AiStepperController();

  void onTapPicker(EasyImagePickerFile? file) async {
    if (file == null) return;
    final bytes = await file.bytes;
    if (bytes == null) return;
    pickedImage.value = AiBytesImage(bytes: bytes);
    step.value = stepWaitSubmit;
    stepController.setActive(step.value);
  }

  void onTapUpload() async {
    final file = await EasyImagePicker.pickSingleImageGrant();
    onTapPicker(file);
  }

  void onTapClear() {
    pickedImage.value = AiBytesImage.empty();
    step.value = stepWaitSelectImage;
    stepController.setActive(step.value);
  }

  void onTapMake() async {
    if (pickedImage.value.isEmpty) {
      AiNoImageTipsDialog().show();
      return;
    }
    AiWxtsImageDialog(
      onConfirm: () {
        final userService = Get.find<UserService>();
        final costType = userService.checkAiCost(
          costAiNum: Consts.aiClothCostCount,
          costGold: Consts.aiClothCostGold,
        );
        if (costType == AiCostType.fail) {
          ApiCode.defaultGoldLackHandler();
        } else if (costType == AiCostType.gold) {
          AiCostGoldConfirmBottomSheet(
            Consts.aiClothCostGold,
            onConfirm: _onMakeConfirm,
          ).show();
        } else {
          _onMakeConfirm();
        }
      },
    ).show();
  }

  void _onMakeConfirm() async {
    final future = Api.createAiClothOffBoxOrder(pickedImage.value.bytes);
    final resp = await FutureLoadingDialog(future).showUnsafe();
    ApiCode.handle(
      resp,
      successToast: '提交成功',
      onSuccess: () {
        step.value = stepSubmitted;
        stepController.setActive(step.value);
      },
    );
  }
}
